package com.jianyun.jyzs.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;

import com.jianyun.jyzs.R;
import com.jianyun.jyzs.activity.SelectEProjectActivity;
import com.jianyun.jyzs.adapter.SelectP1Adapter;
import com.jianyun.jyzs.bean.EngineerProjectResult;
import com.jianyun.jyzs.common.ITextWatchListener;
import com.jianyun.jyzs.dao.EngProjectDao;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by yangshijun on 2018/5/31.
 * 选择项目---》全部项目
 */

@SuppressLint("ValidFragment")
public class P1FragmentAllFragment extends Fragment {
    @BindView(R.id.search_edit)
    EditText searchEdit;
    @BindView(R.id.iv_search)
    ImageView ivSearch;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private SelectP1Adapter adapter;
    private List<EngineerProjectResult.EngProject> engineer;
    private SelectEProjectActivity activity;
    private EngProjectDao dao;
    private Unbinder bind;

    public P1FragmentAllFragment(SelectEProjectActivity selectEProjectActivity) {
        this.activity = selectEProjectActivity;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_p1_all, null);
        bind = ButterKnife.bind(this, view);
        init();
        return view;
    }

    private void init() {
        dao = new EngProjectDao(getActivity());
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new SelectP1Adapter((SelectEProjectActivity) getActivity(), null);
        recyclerView.setAdapter(adapter);
        engineer = dao.getEngineer(((SelectEProjectActivity) getActivity()).enterpriseCode);
        adapter.setList(engineer);
        adapter.setSelectNextLevelListener(new SelectP1Adapter.SelectNextLevelListener() {
            @Override
            public void toNextLevel(EngineerProjectResult.EngProject project) {
                searchEdit.setText("");
                activity.setNextLevelView(project);
            }
        });
        searchEdit.addTextChangedListener(new ITextWatchListener() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String str = s.toString();
                setP1View(str);
            }
        });
    }

    private void setP1View(String str) {
        if (str == null) {
            engineer = dao.getEngineer(((SelectEProjectActivity) getActivity()).enterpriseCode);
        } else {
            engineer = dao.getEngineerSearch(str, ((SelectEProjectActivity) getActivity()).enterpriseCode);
        }
        adapter.setList(engineer);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        bind.unbind();
    }
}
